﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Collections;

namespace Quadarax.Applications.ASMIDE.Controls
{
    class QTextCacheCollection
    {
       
        private ArrayList m_oSegments = new ArrayList();
        private FileStream m_oStream = null;
        private string m_sFileName = "";
        private QTextMapperCollection m_oMapper = null;

        public void Init(string sFileName, long nInitialSegmentLength)
        {        
            m_sFileName = sFileName;
            FileInfo oInfo = new FileInfo(m_sFileName);            
            m_oStream = new FileStream(m_sFileName, FileMode.OpenOrCreate);
            Init(m_oStream, oInfo.Length, nInitialSegmentLength);
        }
        private void Init(FileStream oFile, long nFileSize, long nInitialSegmentLength)
        {
            m_oStream = oFile;
            m_oSegments.Clear();                        
            int nSegmentsCount = Convert.ToInt32(nFileSize / nInitialSegmentLength);
            for (int a = 0; a < nSegmentsCount; a++)
                m_oSegments.Add(new QTextCache(this,a * nInitialSegmentLength, nInitialSegmentLength));
            ((QTextCache)m_oSegments[0]).Read();
        }

        public string[] GetLines(long nFromLine, long nToLine)
        {
            StreamReader oRd = new StreamReader("lolo");            
            int nSegmentsCount = m_oSegments.Count;
            int nFromSegment = 0;
            int nToSegment = 0;
            StringBuilder oSB = new StringBuilder(Convert.ToInt32(nToPosition - nFromPosition));
            for (int a = 0; a < nSegmentsCount; a++)
            {
                if (((QTextCache)m_oSegments[a]).SegmentPosition <= nFromPosition)
                    nFromSegment = a;
                if (((QTextCache)m_oSegments[a]).SegmentPosition >= nToPosition && nToSegment == 0)
                    nToSegment = a;
            }
            for (int a = nFromSegment; a < nToSegment; a++)
                oSB.Append(((QTextCache)m_oSegments[a]).Data);

            if (m_oStream != null)
                m_oStream.Close();
            return oSB.ToString();
        }


        public FileStream GetStream()
        {
            if (m_oStream == null)
            {
                m_oStream = new FileStream(m_sFileName, FileMode.OpenOrCreate);
                return m_oStream;
            }
            if (m_oStream.CanRead)
                return m_oStream;
            else
            {
                m_oStream = new FileStream(m_sFileName,FileMode.OpenOrCreate);
                return m_oStream;
            }
        }

        public void Dump()
        {
            bool bStreamStatus = m_oStream == null ? false : m_oStream.CanRead;
            System.Diagnostics.Trace.WriteLine(string.Format("QTextCacheCollection : FileName={0}; Count={1}; StreamStatus={2}", m_sFileName, m_oSegments.Count, bStreamStatus));            
            for(int a =0; a<m_oSegments.Count; a++)
                System.Diagnostics.Trace.WriteLine(string.Format("QTextCache[{0}] : DataLen={1}; Status={2}", a, ((QTextCache)m_oSegments[a]).GetDataLength(),((QTextCache)m_oSegments[a]).Status));
        }
    }
}
